import { Request } from './Request';

export class Response {
  constructor(
    private requestInternal: Request,
    private messageInternal: string,
    private codeInternal: number,
    private headersInternal: Map<string, string>,
    private bodyInternal: object
  ) {
  }


  get body(): object {
    return this.bodyInternal
  }


  get headers(): Map<string, string> {
    return this.headersInternal
  }

  get code(): number {
    return this.codeInternal
  }

  get message(): string {
    return this.messageInternal
  }

  get request(): Request {
    return this.requestInternal
  }

  header(name: string, defaultValue: string = null): string {
    return this.headers.get(name) ?? defaultValue
  }
}

export namespace Response {
  export class Builder {
    requestInternal: Request
    messageInternal: string
    codeInternal: number
    headersInternal: Map<string, string> = new Map<string, string>()
    bodyInternal: object

    request(request: Request): Builder {
      this.requestInternal = request
      return this
    }

    message(message: string): Builder {
      this.messageInternal = message
      return this
    }

    header(name: string, value: string): Builder {
      this.headersInternal.set(name, value)
      return this
    }

    code(code: number): Builder {
      this.codeInternal = code
      return this
    }

    removeHeader(name: string): Builder {
      this.headersInternal.delete(name)
      return this
    }

    body(body: object): Builder {
      this.bodyInternal = body
      return this
    }

    build(): Response {
      return new Response(
        this.requestInternal,
        this.messageInternal,
        this.codeInternal,
        this.headersInternal,
        this.bodyInternal
      )
    }
  }
}